package com.rose.order.config;

import com.rabbitmq.client.Channel;
import com.rose.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author rose
 * @create 2022/3/11
 */
@Configuration
public class MyMQConfig {

    @Value("${myRabbitmq.MQConfig.queues}")
    private String queues;

    @Value("${myRabbitmq.MQConfig.eventExchange}")
    private String eventExchange;

    @Value("${myRabbitmq.MQConfig.routingKey}")
    private String routingKey;

    @Value("${myRabbitmq.MQConfig.delayQueue}")
    private String delayQueue;

    @Value("${myRabbitmq.MQConfig.createOrder}")
    private String createOrder;

    @Value("${myRabbitmq.MQConfig.ReleaseOther}")
    private String ReleaseOther;

    @Value("${myRabbitmq.MQConfig.ReleaseOtherKey}")
    private String ReleaseOtherKey;

    @Value("${myRabbitmq.MQConfig.ttl}")
    private String ttl;

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单数据，准备关闭订单");
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//    }
//

    /**
     * 容器中的组件会自动的创建
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        Map<String ,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", eventExchange);
        arguments.put("x-dead-letter-routing-key", routingKey);
        arguments.put("x-message-ttl", 60000);
        Queue queue = new Queue(delayQueue, true, false, false, arguments);
        return queue;

    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue(queues, true, false, false);
        return queue;
    }
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(eventExchange, true, false);
    }
    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding(delayQueue, Binding.DestinationType.QUEUE, eventExchange, createOrder, null);
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding(queues, Binding.DestinationType.QUEUE, eventExchange, routingKey, null);
    }

    /**
     * 订单释放直接和库存进行绑定
     * @return
     */
//    @Bean
//    public Binding orderReleaseOtherBinding(){
//        return new Binding(queues, Binding.DestinationType.QUEUE, eventExchange, routingKey, null);
//    }
    @Bean
    public Binding orderReleaseOtherBinding(){
        // ReleaseOther 绑定的解锁队列 eventExchange 绑定的交换机  ReleaseOtherKey 一个模糊匹配的路由键
        return new Binding(ReleaseOther, Binding.DestinationType.QUEUE, eventExchange, ReleaseOtherKey + ".#", null);
    }

    @Bean
    public Queue orderSecKillQueue(){
        return new Queue("order.seckill.order.queue", true, false, false);
    }

    @Bean
    public Binding orderSecKillQueueBinding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }


}
